package com.lqwu.mallchatserver.common.event.listener;

import com.lqwu.mallchatserver.common.event.UserOnlineEvent;
import com.lqwu.mallchatserver.user.model.entry.User;
import com.lqwu.mallchatserver.user.service.IpService;
import com.lqwu.mallchatserver.user.service.UserService;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 用户上线监听器
 */
@Component
public class UserOnlineListener {
    @Resource
    private UserService userService;
    @Resource
    private IpService ipService;

    @EventListener(classes = UserOnlineEvent.class)
    public void saveDB(UserOnlineEvent event) {
        User user = event.getUser();
        User updateUser = new User();
        updateUser.setId(user.getId());
        updateUser.setLastOptTime(user.getLastOptTime());
        updateUser.setActiveStatus(user.getActiveStatus());
        updateUser.setIpInfo(user.getIpInfo());
        userService.updateById(updateUser);
        // 用户解析 ip 信息
        ipService.refreshIpDetailAsync(user.getId());
    }
}
